package com.ruoyi.human.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.human.domain.AdverseEventReport;
import com.ruoyi.human.domain.HumanClinicalTrial;
import com.ruoyi.human.domain.ParticipantHealthMonitoring;
import com.ruoyi.human.service.IHumanClinicalTrialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.domain.ClinicalTrialEmergencyPlan;
import com.ruoyi.human.service.IClinicalTrialEmergencyPlanService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 临床试验应急预案Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/plan")
public class ClinicalTrialEmergencyPlanController extends BaseController
{
    @Autowired
    private IClinicalTrialEmergencyPlanService clinicalTrialEmergencyPlanService;
    @Autowired
    private IHumanClinicalTrialService humanClinicalTrialService;
    /**
     * 查询临床试验应急预案列表
     */
    @RequiresPermissions("human:plan:list")
    @GetMapping("/list")
    public TableDataInfo list(ClinicalTrialEmergencyPlan clinicalTrialEmergencyPlan)
    {
        startPage();
        List<ClinicalTrialEmergencyPlan> list = clinicalTrialEmergencyPlanService.selectClinicalTrialEmergencyPlanList(clinicalTrialEmergencyPlan);
        for (ClinicalTrialEmergencyPlan clinicalTrialEmergencyPlan1:list){
            Long trialId=clinicalTrialEmergencyPlan1.getTrialId();
            HumanClinicalTrial humanClinicalTrial=humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId);
            if(humanClinicalTrial!=null){
                clinicalTrialEmergencyPlan1.setTrialName(humanClinicalTrial.getTrialName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出临床试验应急预案列表
     */
    @RequiresPermissions("human:plan:export")
    @Log(title = "临床试验应急预案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClinicalTrialEmergencyPlan clinicalTrialEmergencyPlan)
    {
        List<ClinicalTrialEmergencyPlan> list = clinicalTrialEmergencyPlanService.selectClinicalTrialEmergencyPlanList(clinicalTrialEmergencyPlan);
        ExcelUtil<ClinicalTrialEmergencyPlan> util = new ExcelUtil<ClinicalTrialEmergencyPlan>(ClinicalTrialEmergencyPlan.class);
        util.exportExcel(response, list, "临床试验应急预案数据");
    }

    /**
     * 获取临床试验应急预案详细信息
     */
    @RequiresPermissions("human:plan:query")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return success(clinicalTrialEmergencyPlanService.selectClinicalTrialEmergencyPlanByPlanId(planId));
    }

    /**
     * 新增临床试验应急预案
     */
    @RequiresPermissions("human:plan:add")
    @Log(title = "临床试验应急预案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClinicalTrialEmergencyPlan clinicalTrialEmergencyPlan)
    {
        clinicalTrialEmergencyPlan.setCreatedAt(new Date());
        clinicalTrialEmergencyPlan.setCreatedBy(SecurityUtils.getUsername());
        return toAjax(clinicalTrialEmergencyPlanService.insertClinicalTrialEmergencyPlan(clinicalTrialEmergencyPlan));
    }

    /**
     * 修改临床试验应急预案
     */
    @RequiresPermissions("human:plan:edit")
    @Log(title = "临床试验应急预案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClinicalTrialEmergencyPlan clinicalTrialEmergencyPlan)
    {
        clinicalTrialEmergencyPlan.setUpdatedBy(SecurityUtils.getUsername());
        clinicalTrialEmergencyPlan.setUpdatedAt(new Date());
        clinicalTrialEmergencyPlan.setVersion(clinicalTrialEmergencyPlan.getVersion()+1);
        return toAjax(clinicalTrialEmergencyPlanService.updateClinicalTrialEmergencyPlan(clinicalTrialEmergencyPlan));
    }

    /**
     * 删除临床试验应急预案
     */
    @RequiresPermissions("human:plan:remove")
    @Log(title = "临床试验应急预案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        for (Long id:planIds){
            ClinicalTrialEmergencyPlan clinicalTrialEmergencyPlan=clinicalTrialEmergencyPlanService.selectClinicalTrialEmergencyPlanByPlanId(id);
            clinicalTrialEmergencyPlan.setDeletedAt(new Date());
            clinicalTrialEmergencyPlan.setDeletedBy(SecurityUtils.getUsername());
            clinicalTrialEmergencyPlan.setDeletedFlag(1);
            clinicalTrialEmergencyPlanService.updateClinicalTrialEmergencyPlan(clinicalTrialEmergencyPlan);
        }
        return toAjax(true);
    }
}
